package com.one.controller;

import com.one.annotation.RequestPermission;
import com.one.service.SignService;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.utils.SignUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * Create on 2022-01-14
 * <p>
 * Author: LiangWei
 * <p>
 * Description: 签到业务接口
 **/
@RestController
@Slf4j
public class SignController {
    @Autowired
    private SignService signService;
    /**
     *@author : LiangWei
     *@date : 2022-01-14 12:07
     *@return :
     *@description : 签到（包含补签）(重复签到算失败)
     * @RequestParam LocalDate date
     */
    @GetMapping("/doSign")
    public RequestReturn<String> doSign(@RequestParam Integer userId){
        LocalDate localDate = LocalDate.now();
        return signService.doSign(userId,localDate)?RequestReturnFactory.success("签到成功"):RequestReturnFactory.fail("签到失败");
    }
    //@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @RequestParam LocalDate signDate
    //member_sign:202201:1

    /**
     *@author : LiangWei
     *@date : 2022-01-14 16:50
     *@return : java.lang.String
     *@description : 查询该用户当天是否已经签到
     */
    @GetMapping("/checkSign")
    public RequestReturn<String> checkSign(@RequestParam Integer userId,@RequestParam String date){
        LocalDate date1 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return signService.checkSign(userId,date1)?RequestReturnFactory.success("当天已签到"):RequestReturnFactory.success("当天未签到");
    }

    /**
     *@author : LiangWei
     *@date : 2022-01-14 16:50
     *@return :
     *@description : 获取用户某个月的签到次数
     */
    @GetMapping("/getSignCountOneMonth")
    public RequestReturn<Long> getSignCountOneMonth(@RequestParam Integer userId,@RequestParam String date){
        LocalDate date1 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return RequestReturnFactory.success(signService.getSignCountOneMonth(userId,date1));
    }

    /**
     *@author : LiangWei
     *@date : 2022-01-14 16:50
     *@return :
     *@description : 获取用户某个月连续签到次数
     */
    @GetMapping("/getContinuousSignCount")
    public RequestReturn<Long> getContinuousSignCount(@RequestParam Integer userId,@RequestParam String date){
        LocalDate date1 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return RequestReturnFactory.success(signService.getContinuousSignCount(userId,date1));
    }

    /**
     *@author : LiangWei
     *@date : 2022-01-14 16:50
     *@return :
     *@description : 获取用户某个月第一次签到的日期
     */
    @GetMapping("/getFirstSignDate")
    public RequestReturn<LocalDate> getFirstSignDate(@RequestParam Integer userId,@RequestParam String date){
        LocalDate date1 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return RequestReturnFactory.success(signService.getFirstSignDate(userId,date1));
    }

    /**
     *@author : LiangWei
     *@date : 2022-01-14 16:50
     *@return :
     *@description : 获取某个用户一个月的签到流水
     */
    @GetMapping("/getSignInfo")
    public RequestReturn<Map<String,Boolean>> getSignInfo(@RequestParam Integer userId, @RequestParam String date){
        LocalDate date1 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return RequestReturnFactory.success(signService.getSignInfo(userId,date1));
    }

    /**
     *@author : LiangWei
     *@date : 2022-01-14 16:50
     *@return :
     *@description : 获取用户当月最多连续签到次数
     */
    @GetMapping("/getContinuousSignCountByTotalMonth")
    public RequestReturn<Long> getContinuousSignCountByTotalMonth(@RequestParam Integer userId,@RequestParam String date){
        LocalDate date1 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return RequestReturnFactory.success(signService.getContinuousSignCountByTotalMonth(userId,date1));
    }

    /**
     *@author : LiangWei
     *@date : 2022-01-14 16:50
     *@return :
     *@description : 补签(测试用)
     */
    @GetMapping("/BQ")
    public RequestReturn<String> getBQ(@RequestParam Integer userId,@RequestParam String date){
        LocalDate date1 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return RequestReturnFactory.success(signService.doSignBQ(userId,date1));
    }
}
